
/**
 * 提示与加载工具类
 */
export default class Tips {

    constructor() {
        this.isLoading = false;
    }

    /**
     * 弹出提示框
     */
    static ok(title, onHide, icon = 'success') {
        wx.showToast({
            title: title,
            icon: icon,
            mask: true,
            duration: 1500
        });
        //隐藏结束回调
        if (onHide) {
            setTimeout(() => {
                onHide();
            }, 1500);
        }
    }
    /**
     *纯文本 提示框
     */
    static alert_none(title) {
        wx.showToast({
            title: title,
            icon: 'none',
            mask: true,
            duration: 1500
        });
    }
    /**
     * 警告框
     */
    static alert(title) {
        wx.showToast({
            title: title,
            image: '/image/icons/alert.png',
            mask: true,
            duration: 1500
        });
    }

    /**
     * 错误框
     */

    static error(title) {
        wx.showToast({
            title: title,
            image: '/image/icons/error.png',
            mask: true,
            duration: 1500
        });
    }


    /**
     * 弹出加载提示
     */
    static loading(title = '加载中') {
        if (Tips.isLoading) {
            return;
        }
        Tips.isLoading = true;
        wx.showLoading({
            title: title,
            mask: true
        });
    }

    /**
    * 加载完毕
    */
    static loaded() {
        if (Tips.isLoading) {
            Tips.isLoading = false;
            wx.hideLoading();
        }
    }
    /*
    模板消息框
    content 提示内容
    cancel 是否显示 取消按钮
    okurl 点击确定 跳转的位置
     */
    static modal(content,cancel=false,stype,okurl=false,cb=false){
        wx.showModal({
            title: "温馨提示",
            content: content,
            showCancel:cancel,
            success: function(res) {
                if (res.confirm) {
                    if(stype == 'goto'){
                        Tips.goto(okurl)
                    }
                  if (stype == 'guanbi') {
                    Tips.redirectTo(okurl)
                  }
                    if(stype == 'daohang'){
                        Tips.godaohang(okurl)
                    }
                    if(stype='function'&&cb){
                        return  cb()
                    }


                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    }


    static goto(url) {
        wx.navigateTo({
            url: url
        });
    }

    static redirectTo(url) {
        wx.redirectTo({
            url: url
        });
    }

    //返回一次
    static back() {
        wx.navigateBack({
            delta: 1,
        });
    }
    //跳转导航
    static godaohang(url) {
        wx.switchTab({
            url: url
        });
    }
//关闭所有页面
    static closeall(url) {
        wx.reLaunch({
            url: url
        });
    }
}

/**
 * 静态变量，是否加载中
 */
Tips.isLoading = false;